/*
 * @(#)DependencyFigure.java  1.0  18. Juni 2006
 *
 * Copyright (c) 1996-2006 by the original authors of JHotDraw
 * and all its contributors ("JHotDraw.org")
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JHotDraw.org ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with
 * JHotDraw.org.
 */

package com.thoughtworks.studio.tools.cardkit.gui.figures;

import static org.jhotdraw.draw.AttributeKeys.END_DECORATION;
import static org.jhotdraw.draw.AttributeKeys.FONT_ITALIC;
import static org.jhotdraw.draw.AttributeKeys.FONT_UNDERLINE;
import static org.jhotdraw.draw.AttributeKeys.START_DECORATION;
import static org.jhotdraw.draw.AttributeKeys.STROKE_COLOR;
import static org.jhotdraw.draw.AttributeKeys.STROKE_DASHES;
import static org.jhotdraw.draw.AttributeKeys.STROKE_WIDTH;

import java.awt.Color;

import org.jhotdraw.draw.ArrowTip;
import org.jhotdraw.draw.Connector;
import org.jhotdraw.draw.Drawing;
import org.jhotdraw.draw.LineConnectionFigure;

/**
 * DependencyFigure.
 */
public class DependencyFigure extends LineConnectionFigure {
    /**
	 * 
	 */
	private static final long serialVersionUID = -4601951861758699972L;

	/** Creates a new instance. */
    public DependencyFigure() {
        STROKE_COLOR.basicSet(this, new Color(0x000099));
        STROKE_WIDTH.basicSet(this, 1d);
        END_DECORATION.basicSet(this, new ArrowTip());
        
        setAttributeEnabled(END_DECORATION, false);
        setAttributeEnabled(START_DECORATION, false);
        setAttributeEnabled(STROKE_DASHES, false);
        setAttributeEnabled(FONT_ITALIC, false);
        setAttributeEnabled(FONT_UNDERLINE, false);
    }
    
    /**
     * Checks if two figures can be connected. Implement this method
     * to constrain the allowed connections between figures.
     */
   @Override public boolean canConnect(Connector start, Connector end) {
        if ((start.getOwner() instanceof CardFigure)
        && (end.getOwner() instanceof CardFigure)) {
            
            CardFigure sf = (CardFigure) start.getOwner();
            CardFigure ef = (CardFigure) end.getOwner();
            
            // Disallow multiple connections to same dependent
            if (ef.getPredecessors().contains(sf)) {
                return false;
            }
            
            // Disallow cyclic connections
            return ! sf.isDependentOf(ef);
        }
        
        return false;
    }
    @Override public boolean canConnect(Connector start) {
        return (start.getOwner() instanceof CardFigure);
    }
    
    
    /**
     * Handles the disconnection of a connection.
     * Override this method to handle this event.
     */
    @Override protected void handleDisconnect(Connector start, Connector end) {
        CardFigure sf = (CardFigure) start.getOwner();
        CardFigure ef = (CardFigure) end.getOwner();
        
        sf.removeDependency(this);
        ef.removeDependency(this);
    }
    
    /**
     * Handles the connection of a connection.
     * Override this method to handle this event.
     */
    @Override protected void handleConnect(Connector start, Connector end) {
        CardFigure sf = (CardFigure) start.getOwner();
        CardFigure ef = (CardFigure) end.getOwner();
        
        sf.addDependency(this);
        ef.addDependency(this);
    }
    
    public DependencyFigure clone() {
        DependencyFigure that = (DependencyFigure) super.clone();
        
        return that;
    }
    
    public int getLayer() {
        return 1;
    }
    
    @Override public void removeNotify(Drawing d) {
        if (getStartFigure() != null) {
            ((CardFigure) getStartFigure()).removeDependency(this);
        }
        if (getEndFigure() != null) {
            ((CardFigure) getEndFigure()).removeDependency(this);
        }
        super.removeNotify(d);
    }
}
